package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysStudentRank;
import com.ruoyi.system.domain.SysStudentRankHistory;
import com.ruoyi.system.service.SysStudentRankHistoryService;
import com.ruoyi.system.service.SysStudentRankService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统-学生段位Controller
 */
@RestController
@RequestMapping("/system/studentRank")
public class SysStudentRankController {

    @Resource
    private SysStudentRankService sysStudentRankService;

    @Resource
    private SysStudentRankHistoryService sysStudentRankHistoryService;

    /**
     * 接口1：获取当前登录学生的段位信息
     */
    @GetMapping("/currentRank")
    public AjaxResult getCurrentRank() {
        // 1. 使用SecurityUtils获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();

        // 2. 查询段位
        SysStudentRank currentRank = sysStudentRankService.getRankByUserId(userId);

        // 3. 返回结果
        return AjaxResult.success("获取段位成功", currentRank);
    }

    /**
     * 接口2：获取当前登录学生的段位历史（默认查近3个月）
     */
    @GetMapping("/rankHistory")
    public AjaxResult getRankHistory() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();

        // 计算近3个月时间
        Date endTime = new Date();
        Date startTime = new Date(endTime.getTime() - 3L * 30 * 24 * 60 * 60 * 1000);

        // 查询历史
        List<SysStudentRankHistory> historyList = sysStudentRankHistoryService.getRankHistoryByTime(userId, startTime, endTime);
        return AjaxResult.success("获取段位历史成功", historyList);
    }

    /**
     * 接口3：统计当前登录学生的段位变化次数
     */
    @GetMapping("/rankChangeCount")
    public AjaxResult getRankChangeCount() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();

        List<Map<String, Object>> countList = sysStudentRankHistoryService.countRankChange(userId);
        return AjaxResult.success("获取段位变化统计成功", countList);
    }

    /**
     * 接口4：课堂表现积分计入经验值
     */
    @PostMapping("/addExp")
    public AjaxResult addExp(@RequestParam Integer exp) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();
        if (exp == null || exp == 0) {
            return AjaxResult.success("无需变动");
        }
        boolean ok = sysStudentRankService.updateRankExp(userId, exp);
        return ok ? AjaxResult.success("经验值已增加") : AjaxResult.error("经验值更新失败");
    }
}